//
//  SCVideoPlayViewController.m
//  OurMoon
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCVideoPlayViewController.h"
#import "AVPlayer_S.h"
#import "HTTPMethod.h"
#import "SCReplayCountModel.h"
#import "SCReplayView.h"
#import "FitHeader.h"
@interface SCVideoPlayViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)AVPlayer_S *avPlayer;
@end

@implementation SCVideoPlayViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)loadView
{
    [super loadView];
    [self getData];
//    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
//    barView.backgroundColor = [UIColor blackColor];
//    [self.view addSubview:barView];
   
    _avPlayer = [[AVPlayer_S alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * FitHeight) videoURL:_videoListModel.mp4_url];
    [self.view addSubview:_avPlayer];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _avPlayer.frame.size.height, self.view.frame.size.width, 354 * FitHeight) style:UITableViewStylePlain];
    
//    _tableView.tableHeaderView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
//    
//    _tableView.tableHeaderView = avPlayer;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UITableView alloc] init];
    [self.view addSubview:_tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //注意字体大小一定一致
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    
    CGRect rect = [[_dataSourceArray[indexPath.row] b] boundingRectWithSize:CGSizeMake(375 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height + 50 * FitHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SCReplayView *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SCReplayView alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if (_dataSourceArray.count != 0) {
        
        cell.replayCountModel = _dataSourceArray[indexPath.row];
    }
    return cell;
}

- (void)getData
{
    self.dataSourceArray = [NSMutableArray array];
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/hot/video_bbs/%@/0/10/10/2/2", _videoListModel.replyid] BodyDic:nil WithDataBlock:^(id data) {
        if ([data objectForKey:@"hotPosts"] != [NSNull null]) {            
            for (NSDictionary *dic in [data objectForKey:@"hotPosts"]) {
                SCReplayCountModel *model = [[SCReplayCountModel alloc] init];
                [model setValuesForKeysWithDictionary:[dic objectForKey:@"1"]];
                [_dataSourceArray addObject:model];
            }
        }
        [_tableView reloadData];
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_avPlayer.player pause];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
